package com.lennon.oauth.store.impl;

import java.util.HashMap;
import java.util.Map;

import com.lennon.oauth.domain.Client;
import com.lennon.oauth.store.IClientStore;

/**
 * 模拟客户信息仓库
 * 
 * @author lennnon
 *
 */
public final class ClientStore implements IClientStore{

	private Map<String/**clientId*/, Client/**clientInfo*/> clientInfoHolder
		= new HashMap<String, Client>();
	
	{
		clientInfoHolder.put("lennon", new Client("lennon", "secret", "http://localhost:8080/"));
	}
	
	/**
	 * 是否在仓库中包含该clientId
	 * 
	 * @param clientId
	 * @return
	 */
	@Override
	public boolean isContainsClientId(String clientId) {
		return clientInfoHolder.containsKey(clientId);
	}
	
	/**
	 * redirectUri是否以 查询clientId对应的client的redirectUri在同一个域名下
	 * 
	 * @param clientId
	 * @param redirectUri
	 * @return
	 */
	@Override
	public boolean isContainsRedirectUri(String clientId, String redirectUri) {
		Client client =  clientInfoHolder.get(clientId);
		if(client != null) {
			if(redirectUri != null && redirectUri.startsWith(client.getRedirectUri())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean isConatinsClient(String clientId, String clientSecret) {
		if(!isContainsClientId(clientId)) {
			return false;
		}
		if(clientSecret != null && clientSecret.equals(clientInfoHolder.get(clientId).getClientSecret())) {
			return true;
		}
		return false;
	}
}
